package com.itany.corejava.code09_集合.list集合;

import java.util.*;

/**
 * @author 石小俊
 * @date 2023年05月06日 9:38
 */
public class Test07_Vector {

    public static void main(String[] args) {
//        test();
        test01();
        test02();
    }

    public static void test() {
        Vector<Integer> nums = new Vector<>(Arrays.asList(1, 3, 4, 5, 7, 8));
        System.out.println(nums);

        // 普通for循环遍历
        for (int i = 0; i < nums.size(); i++) {
            System.out.print(nums.get(i) + "\t");
        }
        System.out.println();

        // 增强for循环遍历
        for (Integer num : nums) {
            System.out.print(num + "\t");
        }
        System.out.println();


        // Iterator迭代器遍历
        Iterator<Integer> it = nums.iterator();
        while (it.hasNext()) {
            Integer num = it.next();
            System.out.print(num + "\t");
        }
        System.out.println();

        // Enumeration遍历
        // 该方式适用于Vector、Hashtable、Properties等集合
        // Vector支持,ArrayList不支持
        Enumeration<Integer> en = nums.elements();
        // hasMoreElements():判断是否有下一个元素,相当于it.hashNext()
        while (en.hasMoreElements()) {
            // nextElement():获取下一个元素,相当于it.next()
            Integer num = en.nextElement();
            System.out.print(num + "\t");
        }

    }

    public static void test01() {
        ArrayList<Integer> list = new ArrayList<>();
        long begin = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++) {
            list.add(i);
        }

        long end = System.currentTimeMillis();
        System.out.println("ArrayList添加100W条数据耗费了" + (end - begin) + "毫秒");
    }

    public static void test02() {
        Vector<Integer> list = new Vector<>();
        long begin = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++) {
            list.add(i);
        }

        long end = System.currentTimeMillis();
        System.out.println("Vector添加100W条数据耗费了" + (end - begin) + "毫秒");
    }

}
